#!/bin/bash
#
# (c) Copyright, 2009-2010 By Jenkins. 
#
# $Id$

N='\E[m'
RED='\E[0;31m'
GREEN='\E[0;32m'
## ${GREEN}


# Deps
function install_deps {
echo
echo -e "${GREEN}Installing deps...$N"
if [ -x /usr/bin/dpkg ];then
   check_rtorrent=`whereis rtorrent |awk -F":" {'print $2'}`
   if [ $check_rtorrent ];then
      echo -e "${RED}You already have rtorrent installed in:$N $check_rtorrent"
      echo -e "${GREEN}if you have all needed dependencies installed please run: $0 nodeps$N"
      echo -e "${GREEN}if you continue here current rtorrent will be replaced.$N"
      echo -n "Is this ok ? Y|N :"
      read answer junk
         case $answer in
         Y|y|yes|YES|Yes)
            echo -e "${GREEN}good...$N"
         ;;
         N|n|no|NO|No)
            exit 1
         ;;
         esac
   fi
else
   echo -e "${RED}This is not a Debian based system$N"
   echo -e "${GREEN}if you have all needed dependencies installed please run: $0 nodeps$N"
   echo -e "${RED}ubuntu dependencies needed:$N build-essential libtool automake openssl libsigc++-2.0-dev libncurses5-dev libncursesw5-dev libcurl4-openssl-dev libcppunit-dev apache2 libapache2-mod-scgi php5 php5-cli screen p7zip-rar libimdb-film-perl libxml-treepp-perl$N"
   echo
   echo -e "${RED}dpkg not found, cant install deps... $N"
   exit 1
fi

if [ -x /usr/bin/apt-get ];then
#   sudo apt-get build-dep rtorrent libtorrent xmlrpc-c -y
echo -e "${GREEN}Installing needed dependencies...$N"
echo
   sudo apt-get update &>> ~/tsp-install.log
   sudo apt-get install curl subversion build-essential libtool automake openssl libsigc++-2.0-dev \
   php5-geoip unrar libncurses5-dev libncursesw5-dev libcurl4-openssl-dev libcppunit-dev apache2 \
   libapache2-mod-scgi php5 php5-cli screen p7zip-rar libimdb-film-perl libxml-treepp-perl ssh -y 
echo
echo -e "${GREEN}Removing old rtorrent pkg's...$N"
   sudo apt-get remove rtorrent libtorrent* libxmlrpc-c3 libxmlrpc-c3-dev libxmlrpc-core-c3 libxmlrpc-core-c3-dev -y
echo
else
   echo -e "${RED}apt-get not found, cant install deps$N"
   exit 1
fi

}

function install_rt-tsp {
cd
echo
echo -e "${GREEN}Installing rt torrent standard platform...$N"
id rt &>/dev/null
if [ $? = 0 ];then
   echo -e "${RED}user rt exists, please remove and delete or move current homedir out of the way...$N"
   exit 1
else 
   sudo adduser --gecos "tsp system user" --disabled-login --disabled-password rt
fi
[ -d /home/rt/.svn ] || sudo -u rt svn checkout http://rt-tsp.googlecode.com/svn/trunk/rt/ /home/rt &>> ~/tsp-install.log
[ -L /etc/init.d/rtorrentInit.sh ] || sudo ln -sf /home/rt/bin/rtorrentInit.sh /etc/init.d/rtorrentInit.sh
[ -L /etc/rc3.d/S20rtorrentInit.sh ] || sudo update-rc.d rtorrentInit.sh defaults
echo
echo -e "${GREEN}Copying .rtorrent.rc please open ports 20000-20010 tcp/udp in your firewall.$N"
if [ -f /home/rt/.rtorrent.rc ];then
echo -e "${GREEN}If you want to use your existing config please check differences and update accordingly.$N"
echo -e "${GREEN}It is best to say y here, a backup of your old one will be created as .rtorrent.rc~ ...$N"
   sudo -u rt cp --backup=simple -i /home/rt/.rtorrent.rc.example /home/rt/.rtorrent.rc
else
   sudo -u rt cp /home/rt/.rtorrent.rc.example /home/rt/.rtorrent.rc
fi
sudo -u rt cp --backup=simple /home/rt/.autosort.rc.example /home/rt/.autosort.rc

# Download env
function DlBase {
echo
echo -e "${GREEN}Where do you want your downloads folder?$N"
echo -e "${GREEN}This is the temporary data not where to actually store downloaded stuff (at least 50GB disk space)$N"
echo -e "${GREEN}performance note, it is good to have this on a other disk than store$N"
echo -e "${GREEN}If you enter /media/big here a /media/big/Downloads folder will be created...$N"
echo -n "Base Download folder: "
read DlBase junk

}
while [ -z "$DlBase" ] ;
do
   DlBase
done

DlBase=$DlBase/Downloads
echo
echo -e "${GREEN}Creating Downloads folder structure...$N"
for i in moviez gamez default appz Anime Music TVSeries seed ;do
   if [ ! -d $DlBase/$i ];then
      sudo mkdir -vp $DlBase/$i
   fi
done
echo
echo -e "${GREEN}Setting perms rt:rt and 2775 for Downloads folder$N"
sudo chown -R rt:rt $DlBase
sudo chmod -R 2775 $DlBase
sudo ln -sf $DlBase /home/rt/Downloads

function SfBase {
echo
echo -e "${GREEN}Where do you want your downloads to be Stored ?$N"
echo -e "${GREEN}This is where to actually store downloaded stuff (disk space: lots...)$N"
echo -e "${GREEN}This should be an empty or new folder$N"
echo -e "${GREEN}If you want to store it in /media/big/{Apps,Movies,TVSeries} then enter /media/big here$N"
echo -e "${GREEN}The folders {Apps, Movies/{New, DVDR, Bluray, DVDRip}, TVSeries, Games, Anime, Default}$N"
echo -e "${GREEN}will be created automatic whit correct perms for users in group rt$N"
echo -e "${GREEN}just export this folder whit samba or nfs for easy access$N"
echo -e "${GREEN}a link to the torrents folder in /home/rt will be created here to...$N"
echo -n "Base Store folder: "
read SfBase junk

}
while [ -z "$SfBase" ] ;
do
   SfBase
done

#SfBase=$SfBase/Downloads
echo
echo -e "${GREEN}Creating store folder structure and configuring autosort function...$N"
for i in Movies Games Apps Anime Music TVSeries Default ;do
   if [ ! -d $SfBase/$i ];then
      if [[ $SfBase/$i =~ Movies ]];then
         sudo mkdir -vp $SfBase/$i/{New,DVDR,Bluray,DVDRip}
      else
         sudo mkdir -vp $SfBase/$i
      fi
      sudo chown -R rt:rt $SfBase/$i
      sudo chmod -R 2775 $SfBase/$i
      sedrepl=$SfBase/$i
      sedrepl=${sedrepl//\//\\/}
      sudo -u rt sed -i -e "s/${i}tag/$sedrepl/g" /home/rt/.autosort.rc
      sudo -u rt sed -i -e "s/${i}tag/$sedrepl/g" /home/rt/.flexget/config.yml.example
   fi
done

sudo ln -sf /home/rt/torrents $SfBase/
sudo chown rt. /home/rt/torrents
sudo chmod 2777 /home/rt/torrents

echo
echo -e "${GREEN}Hove many days do you want to seed torrents ?$N"
echo -e "${GREEN}Recommended is at least 7 days, min is 1 day$N"
echo -n "seeddays: "
read seeddays junk
[ -z $seeddays ] && seeddays=5
sudo -u rt sed -i -e "s/seeddaystag/$seeddays/g" /home/rt/.autosort.rc

# screen 
echo
echo -e "${GREEN}Configuring screen for multi user$N"
sudo -u rt cp /home/rt/.screenrc.example /home/rt/.screenrc
sudo chmod 755 /var/run/screen
sudo chmod u+s /usr/bin/screen
echo
echo -e "${GREEN}Adding user `id -un` as rt member and screen user...$N"
echo "acladd `id -un`" |sudo -u rt tee -a /home/rt/.screenrc >/dev/null
sudo adduser `id -un` rt

echo -e "${GREEN}Setting rt's umask to 002 for compatibility, you should change this in /etc/.profile to.$N"
echo "umask 002" |sudo -u rt tee -a /home/rt/.profile >/dev/null


# Apache stuff
echo
echo -e "${GREEN}Enabling scgi...$N"
sudo a2enmod scgi
grep SCGIMount /etc/apache2/httpd.conf >/dev/null
[ $? = 0 ] || echo "SCGIMount /RPC2 127.0.0.1:5000" | sudo tee -a /etc/apache2/httpd.conf >/dev/null
sudo /etc/init.d/apache2 restart

sudo crontab -u rt -l |grep TSP >/dev/null
if [ $? = 0 ];then
   echo
   echo -e "${RED}rt.crontab seems already installed check this!$N"
else
   echo
   echo -e "${GREEN}Installing rt.crontab into rt:s crontab...$N"
   sudo crontab -u rt /home/rt/rt.crontab
fi


sudo grep "rt      ALL=NOPASSWD: /sbin/iptables, /usr/sbin/logrotate /home/rt/rt.logrot, /home/rt/bin/setperms" /etc/sudoers >/dev/null
if [ $? = 0 ];then
   echo
   echo -e "${GREEN}sudo for rt already installed...$N"
else
   echo
   echo -e "${GREEN}Installing needed sudo commands for rt...$N"
   echo "rt      ALL=NOPASSWD: /sbin/iptables, /usr/sbin/logrotate /home/rt/rt.logrot, /home/rt/bin/setperms" |sudo tee -a /etc/sudoers >/dev/null
fi

# End install_rt-tsp
}

# Install xmlrpc-c...
function install_xmlrpc-c {
echo
echo -e "${GREEN}Installing xmlrpc-c-svn...$N"
svn co http://xmlrpc-c.svn.sourceforge.net/svnroot/xmlrpc-c/advanced /tmp/xmlrpc-c &>> ~/tsp-install.log
cd /tmp/xmlrpc-c
echo -e "${GREEN}Configuring xmlrpc-c-svn...$N"
./configure --enable-tools &>> ~/tsp-install.log
echo -e "${GREEN}Compiling xmlrpc-c-svn...$N"
make &>> ~/tsp-install.log
[ $? == 0 ] && sudo make install &>> ~/tsp-install.log
sudo ln -sf /usr/local/bin/xmlrpc /usr/bin/xmlrpc
cd
rm -rf /tmp/xmlrpc-c
}
# Install Rtorrent and libtorrent...

function install_libtorrent {
echo
echo -e "${GREEN}Installing libtorrent-0.12.6...$N"
cd /tmp
wget http://libtorrent.rakshasa.no/downloads/libtorrent-0.12.6.tar.gz
tar xzf libtorrent-0.12.6.tar.gz
rm -rf /tmp/libtorrent-0.12.6.tar.gz
cd /tmp/libtorrent-0.12.6
echo -e "${GREEN}Configuring libtorrent-0.12.6...$N"
./configure &>> ~/tsp-install.log
echo -e "${GREEN}Compiling libtorrent-0.12.6...$N"
make &>> ~/tsp-install.log
[ $? == 0 ] && sudo make install &>> ~/tsp-install.log
cd
rm -rf /tmp/libtorrent-0.12.6
}

function install_rtorrent {
echo
echo -e "${GREEN}Installing rtorrent-0.8.6...$N"
cd /tmp
wget http://libtorrent.rakshasa.no/downloads/rtorrent-0.8.6.tar.gz
tar xzf rtorrent-0.8.6.tar.gz
rm -rf /tmp/rtorrent-0.8.6.tar.gz
cd /tmp/rtorrent-0.8.6/
wget http://rt-tsp.googlecode.com/files/rtorrent-0.8.6-canvas-fix.patch
echo -e "${GREEN}Applying canvas patch for colours in rtorrent...$N"
patch -p0 <./rtorrent-0.8.6-canvas-fix.patch
echo -e "${GREEN}Configuring rtorrent-0.8.6...$N"
./configure --with-xmlrpc-c=/usr/local/bin/xmlrpc-c-config  &>> ~/tsp-install.log
echo -e "${GREEN}Compiling rtorrent-0.8.6...$N"
make &>> ~/tsp-install.log
[ $? == 0 ] && sudo make install &>> ~/tsp-install.log
cd
rm -rf /tmp/rtorrent-0.8.6
}

function install_rutorrent {
echo
if [ ! -d /var/www ];then
   echo "no /var/www found where is your web servers document root?"
   echo -n "docroot: "
   read docroot junk

else
   docroot=/var/www

fi

echo -e "${GREEN}Installing rutorrent web-gui from svn to $docroot/rutorrent...$N"
sudo mkdir -p $docroot/rutorrent
sudo chown -R www-data:rt $docroot/rutorrent
sudo chmod 2775 $docroot/rutorrent
sudo svn co http://rutorrent.googlecode.com/svn/trunk/rutorrent $docroot/rutorrent &>> ~/tsp-install.log
echo -e "${GREEN}Installing rutorrent plugins to $docroot/rutorrent/plugins$N"
sudo svn co http://rutorrent.googlecode.com/svn/trunk/plugins/{cpuload,diskspace,_getdir,rss,seedingtime,trafic,data,datadir,erasedata,scheduler,throttle,geoip,extsearch,cookies} $docroot/rutorrent/plugins &>> ~/tsp-install.log
sudo chown -R www-data:rt $docroot/rutorrent
sudo chmod -R g+w $docroot/rutorrent
sudo sed -i -e s/"$forbidUserSettings = false;"/"$forbidUserSettings = true;"/g $docroot/rutorrent/conf/config.php
echo -e "${GREEN}Configuring webserver$N"
cat << EOF | sudo tee $docroot/rutorrent/.htaccess >/dev/null
AuthType Digest
AuthName "rutorrent"
AuthUserFile $docroot/rutorrent/.htpasswd
require valid-user
EOF
sudo a2enmod auth_digest
sudo /etc/init.d/apache2 restart
echo
echo -e "${GREEN}Adding `id -un` as rutorrent user$N"
sudo htdigest -c $docroot/rutorrent/.htpasswd rutorrent `id -un`
echo
echo -e "${RED}To enable auth logins please change AllowOverride none to: AllowOverride AuthConfig in your web servers default config$N"
echo -n "Hit Enter"
read junk

}

function install_flexget {
echo -e "${GREEN}Installing Flexget-svn...$N"
[ -d /home/rt/flexget-dev ] || sudo -u rt svn co http://svn.flexget.com/trunk /home/rt/flexget-dev &>> ~/tsp-install.log
[ $? = 0 ] || (echo -e "${RED}Something went wrong check tsp-install.log$N"; exit 1)
sudo su -c "cd /home/rt/flexget-dev ;python bootstrap.py" rt &>> ~/tsp-install.log
if [ $? = 0 ];then
   echo -e "${GREEN}Installed OK...$N"
   echo -e "${GREEN}Please copy /home/rt/.flexget/config.yml.example to config.yml and edit for your needs$N"
   echo -e "${GREEN}Then run flexget -v --initdb as user rt to initialise the db$N"
   echo -n "Hit Enter"
   read junk
else
   echo -e "${RED}Something went wrong check tsp-install.log$N"
   exit 1
fi

}


case $1 in
      nodeps)
         install_rt-tsp
         install_rutorrent
         install_flexget
         ;;
      noapt)
         install_xmlrpc-c
         install_libtorrent
         install_rtorrent
         install_rt-tsp
         install_rutorrent
         install_flexget
         ;;
      *)
echo
echo -e "${GREEN}        #######  #####  ######
           #    #     # #     #
           #    #       #     #
           #     #####  ######
           #          # #
           #    #     # #
           #     #####  # ${RED}installer version 0.0.1$N"
echo 
echo -e "${GREEN}You are about to install TSP Torrent Standard Platform...$N"
echo -e "${GREEN}This is created for ubuntu linux but should work for any linux
if you install rtorrent 0.8.6+ and xmlrpc-c 0.11.0+ by your self then you can run $0 nodeps
If you want the script to compile rtorrent and xmlrpc-c for you but are not running debian based system
you can run $0 noapt after installing all the necessary building dependences...$N"
echo -n "Hit Enter"
read junk
         install_deps
         install_xmlrpc-c
         install_libtorrent
         install_rtorrent
         install_rt-tsp
         install_rutorrent
         install_flexget
         ;;
esac

echo
echo "you are done."
cd
